const app = getApp();
const req = require("../../utils/http.js");
Page({
  data: {
    page: 1,
    list: [],
    currentIndex: 0,
    openid: '',
    setShow: true,
    eliminate: '',
    number: '',
    page: 1,
    size: 10,
    newList:[]
  },

 //跳转到领券页面
  skip(e){
    let { sendtype, tradetype, value, outtradeno}=e.currentTarget.dataset
    let currentIndex = this.data.currentIndex
    if (currentIndex==2){//已完成页面
      if (tradetype == 30 && sendtype == 0) {//团购券并且未领取
        wx.redirectTo({
          url: '../sendCoupon/sendCoupon?value=' + value + '&outTradeNo=' + outtradeno,
        })
      } else if (tradetype == 30 && sendtype == 1) {
        wx.showToast({
          title: '发券成功，请往微信卡包里查看！',
          icon: 'none',
          duration: 1000
        })
      }
    }
 
   

  },

  onLoad: function (options) {
    let t = this
    let page = t.data.page;
    let size = t.data.size;
    let currentIndex = ''
    switch (options.type) {
      case '1':
        t.setData({
          currentIndex: 2,
          setShow: true,
          number: 2
        })
         currentIndex = 2
        t.request(currentIndex, page,size)
        break;
      case '2':
        t.setData({
          currentIndex: 2,
          setShow: true,
          number: 2
        })
         currentIndex = 2
        t.request(currentIndex, page, size)
        break;
      case '3':
        t.setData({
          currentIndex: 3,
          setShow: true,
          number: 3
        })
         currentIndex = 3
        t.request(currentIndex,page,size)
        break;
      default:
        //进入页面获取全部订单1
        t.setData({
          number: 1
        })
        t.request(0,1,10)
    }
  

  },
  //用户点击tab时调用
  titleClick: function (e) {
    let t = this
    let page = 1
    let size = t.data.size
    if (t.data.currentIndex != e.currentTarget.dataset.idx) {
      t.setData({
        //拿到当前索引并动态改变
        currentIndex: e.currentTarget.dataset.idx,
        // searchPageNum: 1,
        newList: [],
        number: e.currentTarget.dataset.idx
      })
      t.request(e.currentTarget.dataset.idx,page,size)
    }
  },

   //购买
  goBuy: function () {
    wx.redirectTo({
      url: '../index/index',
    })
  },

  request: function (index,page,size) {
    wx.showLoading({
      title: '玩命加载中...',
    })
    let t = this;
    let params ={}
    if (index == 0){
      params={}
    }
    else if (index == 1) { //待付款
      params.orderStatus = 0;
    } else if (index == 2) { //已完成
      params.orderStatus = 1;
    } else if (index == 3) { //退货
      params.orderStatus = 3;
    }

    req.wxReq(`/coupon/wxProduct/selectWxOrder/${page}/${size}`,"POST",params).then(res=>{
      wx.hideLoading();
      let list = res.data.result.records
      let newList = t.data.newList.concat(list)
      t.setData({
        newList,
        total: res.data.result.total
      })
    })
  },


  // 退款
  reimburse(e) {
    let t = this
    let orderid = e.currentTarget.dataset.orderid
    const params = {
      orderId: orderid
    }
    req._objPost('/coupon/wxProduct/refund', "POST", params).then(res => {
      if (res.data.code == 200) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 800
        })

        let index = t.data.currentIndex
        t.setData({
          newList:[]
        })
        setTimeout(()=>{
          t.request(index, 1, 10);
        },900)
       
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  // 退款--取消
  cancel() {
    console.log('退款取消')
    this.setData({
      Showburse: false
    })
  },

  //滚动到底部触发事件
  onReachBottom: function () {
    let t = this
    let index = t.data.currentIndex
    wx.showLoading({
      title: '玩命加载...',
    })
    var page = t.data.page + 1;
    t.setData({
      page: page
    })
    if (page > Math.ceil(t.data.total / t.data.size)) {
      wx.showToast({
        title: '到底了！！！',
        icon: 'none'
      })
      return;
    };
   t.request(index, page, t.data.size);
    wx.hideLoading()

  },
  onShow: function () {
    this.setData({
      page: 1
    })
  },

})
